package cn.ecnu.service.impl;

import cn.ecnu.mapper.ConferenceMapper;
import cn.ecnu.pojo.Conference;
import cn.ecnu.service.ConferenceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConferenceServiceImpl extends ServiceImpl<ConferenceMapper, Conference> implements ConferenceService {
    @Override
    public List<Conference> getTop10ConferencesByViews() {
        QueryWrapper<Conference> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("views");
        queryWrapper.last("LIMIT 10");
        return list(queryWrapper);
    }

    @Override
    public List<Conference> getTop10ConferencesByFollows() {
        QueryWrapper<Conference> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("follows");
        queryWrapper.last("LIMIT 10");
        return list(queryWrapper);
    }

    @Override
    public void incrementViews(int id) {
        Conference conference = getById(id);
        if (conference != null) {
            conference.setViews(conference.getViews() + 1);
            updateById(conference);
        }
    }

    @Override
    public void incrementFollows(Long id) {
        Conference conference = getById(id);
        if (conference != null) {
            conference.setFollows(conference.getFollows() + 1);
            updateById(conference);
        }
    }

    @Override
    public void decrementFollows(Long id) {
        Conference conference = getById(id);
        if (conference != null) {
            conference.setFollows(conference.getFollows() - 1);
            updateById(conference);
        }
    }
}
